package com.markpim.dena.opdracht3;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

public class Main {

	// fields
	private static TrieNode root;

	// constructor
	public Main() {

	}

	public static void main(String[] args) {
		root = new TrieNode(null);

		inputFromFile("bible.txt");

		System.out.println("\nbijbel klaar, overige testinput");
		
		root = new TrieNode(null);
		doTestInput();
	}

	// methods
	public static void inputFromFile(String fileName) {
		try {
			Scanner input = new Scanner(new File(fileName));
			int position = 0;
			while (input.hasNext()) {
				String s = input.next();
				// strip het woord van leestekens
				s = s.toLowerCase();
				s = s.replaceAll("^([\\[\\]]+)", "");
				s = s.replaceAll("([A-Za-z]+)[\\[\\]()0-9?:!.,;'\\#]*", "$1");

				insert(position, s);
				position++;
			}
			System.out.println(position + " words in total");
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		// System.out.println();
		// search("covenantbreakers");
		// System.out.println();
		// search("the");
		// System.out.println();
		printAllData();

		Collection<Data> allDataFreq = getAllDataFrequency();
		writeToCsvFrequencyTable("bibleFrequency.csv", allDataFreq);
		TreeSet<Data> allDataAlph = getAllDataAlphabetically();
		writeToCsvAlphabeticalTable("bibleAlphabetical.csv", allDataAlph);
	}

	public static void doTestInput() {
		// Het meeste testen is gedaan door in deze testInput functie dingen toe
		// te voegen, dingen te verwijderen en dingen op te zoeken en om hierna
		// in de gegenereerde csv's te kijken of alles klopt
		insert(0, "ball");
		insert(3, "ball");
		insert(1, "ball");
		insert(1, "ball");
		insert(0, "bat");
		insert(0, "doll");
		insert(0, "dork");
		insert(0, "dork");
		insert(0, "do");
		insert(0, "dorm");
		insert(0, "send");
		insert(0, "sense");

			if (search("ballasdf") == null) {
				System.out.println("search 1 goed");
			}
			if (search("do") != null && search("do").equals("do")) {
				System.out.println("search 2 goed");
			}

			if(search("send") != null && search("send").equals("send")) {
				System.out.println("het woord 'send' is gevonden");
			} else if(search("send") == null) {
				System.out.println("het woord is niet gevonden");
			}
			
		delete("do");
		delete("send");
			
			if(search("do") != null && search("do").equals("do")) {
				System.out.println("het woord 'do' is gevonden");
			} else if(search("do") == null) {
				System.out.println("het woord is niet gevonden");
			}
			if(search("send") != null && search("send").equals("send")) {
				System.out.println("het woord 'send' is gevonden");
			} else if(search("send") == null) {
				System.out.println("het woord is niet gevonden");
			}

		insert(0, "sende");
		
			if(search("sende") != null && search("sende").equals("sende")) {
				System.out.println("het woord 'sende' is gevonden");
			} else if(search("sende") == null) {
				System.out.println("het woord is niet gevonden");
			}

		delete("sen");

		System.out.println();
		printAllData();

		Collection<Data> allDataFreq = getAllDataFrequency();
		writeToCsvFrequencyTable("testFrequency.csv", allDataFreq);
		TreeSet<Data> allDataAlph = getAllDataAlphabetically();
		writeToCsvAlphabeticalTable("testAlphabetical.csv", allDataAlph);
	}

	public static void writeToCsvAlphabeticalTable(String fileName,
			TreeSet<Data> data) {
		FileWriter fw = null;
		try {
			fw = new FileWriter(fileName);
			fw.append("word");
			fw.append(",");
			fw.append("frequency");
			fw.append(",");
			fw.append("positions");
			fw.append("\n");

			for (Data d : data) {
				fw.append(d.getWord());
				fw.append(",");
				fw.append("" + d.getFreq());
				fw.append(",");
				for (int i : d.getPosition()) {
					fw.append(i + "  ");
				}
				fw.append("\n");
			}
			fw.flush();
			fw.close();
			System.out.println("alphabetical csv table created");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void writeToCsvFrequencyTable(String fileName,
			Collection<Data> data) {
		FileWriter fw = null;
		try {
			fw = new FileWriter(fileName);
			fw.append("word");
			fw.append(",");
			fw.append("total count");
			fw.append("\n");
			for (Data d : data) {
				fw.append(d.getWord());
				fw.append(",");
				fw.append("" + d.getFreq());
				fw.append("\n");
			}
			fw.flush();
			fw.close();
			System.out.println("frequency csv table created");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void insert(int position, String word) {
		Data d = new Data(position, word);
		root.insert(word, d);
	}

	public static void delete(String word) {
		root.deleteAll(word);
	}

	public static String search(String word) {
		if (root.search(word) != null) {
			return root.search(word).getData().getWord();
		}
		return null;
	}

	public static void printAllData() {
		Collection<Data> dataList = new TreeSet<Data>();
		root.getAllData((TreeSet<Data>) dataList);
		System.out.println("total amount of unique words: " + dataList.size());
	}

	public static TreeSet<Data> getAllDataAlphabetically() {
		TreeSet<Data> dataList = new TreeSet<Data>();
		root.getAllData(dataList);
		return dataList;
	}

	public static ArrayList<Data> getAllDataFrequency() {
		ArrayList<Data> dataList = new ArrayList<Data>();
		root.getAllData(dataList);
		Collections.sort(dataList, new Comparator<Data>() {

			@Override
			public int compare(Data o1, Data o2) {
				return o2.getFreq() - o1.getFreq();
			}

		});
		return dataList;
	}

}
